package com.zxy.zxyrpc.registry;

import com.zxy.zxyrpc.spi.SpiLoader;

/**
 * 注册中心工厂
 *
 * @author zxy
 * @since 2025/5/18
 **/
public class RegistryFactory {
    // 默认注册中心
    private static final Registry DEFAULT_REGISTRY = new EtcdRegistry();
    private static volatile RegistryFactory registryFactory;

    /* 在 SPI 中的 getInstance 方法动态加载可能会更好
    static {
        SpiLoader.load(Registry.class);
    }*/

    private RegistryFactory() {}

    /**
     * 获取实例（懒加载-双检查锁机制）
     *
     * @param key 注册中心关键词
     * @return 注册中心实例
     */
    public static Registry getInstance(String key) {
        if (registryFactory == null) {
            synchronized (RegistryFactory.class) {
                if (registryFactory == null)
                    registryFactory = new RegistryFactory();
            }
        }
        return SpiLoader.getInstance(Registry.class, key);
    }
}
